package com.tech.koufu.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

//获得软件的全局配置文件
public class SharedConfig {
	Context context;
	SharedPreferences shared;
	public SharedConfig(Context context){
		this.context=context;
		shared=context.getSharedPreferences("config", Context.MODE_PRIVATE);
	}

	public SharedPreferences GetConfig(){
		return shared;
	}
	public void ClearConfig(){
		shared.edit().clear().commit();
	}
	
	
	public static String[]  getLoginStatus(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(Statics.SHARE_USER_LOG, Context.MODE_PRIVATE);
		String isLogins = sharedPreferences.getString(Statics.SHARE_USER_ISLOGIN, "-1");
		Log.d(SharedConfig.class.getName(), isLogins+"~~~~~3~~~~~~~~~");
		if(!isLogins.equals("-1")){
			String[] isLoginss = isLogins.split(",");
			if(isLoginss.length==5 && isLoginss[2].equals("1")){
				isLoginss[1] = CoFoolAes.aesEncode(isLoginss[1] , Statics.SAS_CONTENT_KEY);			
				return isLoginss;
			}
		}
		return null;
	}
	
	public static void setLoginStatus(Context context,String uname,String pwd,long nmisstime,String plat){
		SharedPreferences sharedPreferences = context.getSharedPreferences(Statics.SHARE_USER_LOG, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		String encryptResultStr =CoFoolAes.aesDecode(pwd, Statics.SAS_CONTENT_KEY);  
		editor.putString(Statics.SHARE_USER_ISLOGIN, uname+","+encryptResultStr+","+"1"+","+nmisstime+","+plat); 
		editor.commit();
	}
	
	public static void clearLoginStatus(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(Statics.SHARE_USER_LOG, Context.MODE_PRIVATE);
		String aaau = sharedPreferences.getString(Statics.SHARE_USER_ISLOGIN, "");
		if(!aaau.equals("")){
			String[] aaaus = aaau.split(",");
			Editor editor = sharedPreferences.edit(); 
			editor.putString(Statics.SHARE_USER_ISLOGIN, aaaus[0]+","+aaaus[1]+","+"0"+","+System.currentTimeMillis()); 
			editor.commit();
		} 
	}
	public static String[] getLoginShares(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(Statics.SHARE_USER_LOG, Context.MODE_PRIVATE);
		String aaau = sharedPreferences.getString(Statics.SHARE_USER_ISLOGIN, null);
		if(null!=aaau){
			String[] aaaus = aaau.split(",");
			aaaus[1] =  CoFoolAes.aesEncode(aaaus[1] , Statics.SAS_CONTENT_KEY); 
			return aaaus;
		}
		return null;
	}
}
